{$A8,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N+,O+,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y+,Z1}
{$WARN UNSAFE_TYPE OFF}

unit GrfFileInterface;

interface

  uses
    Interface_TLB, ComObj, grftypes;

  type
    TGrfFileInterface = class(TAutoIntfObject, IGrfFileIntf, IDispatch)
      private
        fGrf : PGrf;
        fGrfFile : TGrfFile;
        fIndex : Cardinal;
        function Get_GrfPointer() : Cardinal;
      protected
        function Get_Index: LongWord; safecall;
        function Get_Name: WideString; safecall;
        function Get_isFolder: WordBool; safecall;
        procedure Delete; safecall;
        function Get_Size: LongWord; safecall;
        function Get_compressedSize: LongWord; safecall;
        function Get_Handle: LongWord; safecall;
        property Index: LongWord read Get_Index;
        property Name: WideString read Get_Name;
        property isFolder: WordBool read Get_isFolder;
        property Size: LongWord read Get_Size;
        property compressedSize: LongWord read Get_compressedSize;
        property Handle: LongWord read Get_Handle;
      public
        constructor Create(Grf : PGrf; var GrfFile : TGrfFile; Index : Cardinal);
        destructor Destroy(); override;
        property GrfPointer : Cardinal read Get_GrfPointer;
        property GrfFileIndex : Cardinal read fIndex;
    end;

implementation

  uses
    ActiveX, StringUtils, grf, Dialogs;

  constructor TGrfFileInterface.Create(Grf : PGrf; var GrfFile: TGrfFile; Index : Cardinal);
  var
    TypeLib : ITypeLib;
    ExeName : WideString;
  begin
    //Get name of application
    ExeName := ParamStr(0);
    //Load type library from application's resources
    OleCheck(LoadTypeLib(PWideChar(ExeName), TypeLib));
    //Inherit
    inherited Create(TypeLib, IGrfFileIntf);
    //Set Grf and Grf file
    fGrf := Grf;
    fGrfFile := GrfFile;
    fIndex := Index;
  end;

  destructor TGrfFileInterface.Destroy();
  begin
    inherited;
  end;

  function TGrfFileInterface.Get_GrfPointer() : Cardinal;
  begin
    if fGrf <> nil then
      Result := Cardinal(fGrf)
    else
      Result := 0;
  end;

  function TGrfFileInterface.Get_Index() : Cardinal;
  begin
    Result := fIndex;
  end;

  function TGrfFileInterface.Get_Name() : Widestring;
  begin
    try
      Result := KoreanToUnicode(fGrfFile.name);
    except
      try
        Result := Utf8ToWidestring(fGrfFile.name);
      except
        Result := '';
      end;
    end;
  end;

  function TGrfFileInterface.Get_isFolder() : Wordbool;
  begin
    try
      Result := GRFFILE_IS_DIR(fGrfFile);
    except
      Result := false;
    end;
  end;

  procedure TGrfFileInterface.Delete();
  var
    GrfError : TGrfError;
  begin
    try
      grf_index_del(fGrf, fIndex, GrfError);
    except end;
  end;

  function TGrfFileInterface.Get_Size() : Cardinal;
  begin
    try
      Result := fGrfFile.real_len;
    except
      Result := 0;
    end;
  end;

  function TGrfFileInterface.Get_compressedSize() : Cardinal;
  begin
    try
      Result := fGrfFile.compressed_len;
    except
      Result := 0;
    end;
  end;

  function TGrfFileInterface.Get_Handle() : Cardinal;
  begin
    Result := Cardinal(Self);
  end;

end.
